﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using Microsoft.SharePoint;

namespace SPQueryToSql.ApplicationObjects.CAML
{
    internal class Eq : BaseOperator
    {
        public Eq(XContainer element, IEnumerable<FieldInfo> listFields)
            : base(element, listFields)
        {
        }

        public override string CAMLElementName
        {
            get { return "Eq"; }
        }

        public override string ToSql(Query query)
        {
            return ToSql(query, FieldInfo.InternalName);
        }

        public override string ToSql(Query query, string columnName)
        {
            var sql = new StringBuilder();
            switch (FieldInfo.Type)
            {
                case SPFieldType.Text:
                    sql.AppendFormat("[{0}] = '{1}'", columnName, Value.Text);
                    break;
                case SPFieldType.Note:
                    sql.AppendFormat("[{0}] = '{1}'", columnName, Value.Text);
                    break;
                case SPFieldType.DateTime:
                    sql.AppendFormat("[{0}] = '{1}'", columnName, Value.Text);
                    break;
                case SPFieldType.Choice:
                    sql.AppendFormat("[{0}] = '{1}'", columnName, Value.Text);
                    break;
                case SPFieldType.URL:
                    sql.AppendFormat("[{0}] = '{1}'", columnName, Value.Text);
                    break;
                case SPFieldType.Integer:
                    sql.AppendFormat("[{0}] = {1}", columnName, Value.Text);
                    break;
                case SPFieldType.Counter:
                    sql.AppendFormat("[{0}] = {1}", columnName, Value.Text);
                    break;
                case SPFieldType.Boolean:
                    sql.AppendFormat("[{0}] = {1}", columnName, Value.Text.Equals("TRUE", StringComparison.InvariantCultureIgnoreCase) ? "1" : "0");
                    break;
                case SPFieldType.Number:
                    sql.AppendFormat("[{0}] = {1}", columnName, Value.Text);
                    break;
                case SPFieldType.Currency:
                    sql.AppendFormat("[{0}] = {1}", columnName, Value.Text);
                    break;
                case SPFieldType.Attachments:
                    break;
                case SPFieldType.MaxItems:
                    sql.AppendFormat("[{0}] = {1}", columnName, Value.Text);
                    break;
                case SPFieldType.ModStat:

                    break;
                case SPFieldType.Lookup:
                    if (FieldRef.LookupId == true)
                    {
                        sql.AppendFormat(
                            FieldInfo.IsMulti ? "[{0}] LIKE '%;#%{1}%;#%;#%' OR [{0}] LIKE '%[0-9]%;#{1};#%'" : "[{0}] LIKE '%{1}%;#%;#'",
                            columnName, Value.Text);
                    }
                    else
                    {
                        sql.AppendFormat(
                            FieldInfo.IsMulti ? "[{0}] LIKE '%;#%[0-9]%;#{1};#%' OR [{0}] LIKE '%[0-9]%;#{1};#%'" : "[{0}] LIKE '%[0-9]%;#{1};#'",
                            columnName, Value.Text);
                    }
                    break;
                case SPFieldType.Computed:
                    sql.AppendFormat("[{0}] = '{1}'", columnName, Value.Text);
                    break;
                case SPFieldType.MultiChoice:
                    sql.AppendFormat("[{0}] LIKE '%;#%{1}%;#%'", columnName, Value.Text);
                    break;
                case SPFieldType.Calculated:
                    sql.AppendFormat("[{0}] = '{1}'", columnName, Value.Text);
                    break;
                case SPFieldType.User:
                    if (FieldRef.LookupId == true)
                    {
                        sql.AppendFormat(
                            FieldInfo.IsMulti ? "[{0}] LIKE '%;#%{1}%;#%;#%' OR [{0}] LIKE '%{1}%;#%;#%'" : "[{0}] LIKE '{1}%;#%'",
                            columnName, Value.Text);
                    }
                    else
                    {
                        sql.AppendFormat(
                            FieldInfo.IsMulti ? "[{0}] LIKE '%;#%[0-9]%;#{1};#%' OR [{0}] LIKE '%[0-9]%;#{1};#%'" : "[{0}] LIKE '[0-9]%;#{1}'",
                            columnName, Value.Text);
                    }
                    break;
                default:
                    sql.Append("1=1");
                    break;
            }
            return sql.ToString();
        }
    }
}
